package de.klingbeil.hhc.client.widget;

import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.layout.ColumnLayout;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;

import de.klingbeil.hhc.client.ItemEditorMessages;
import de.klingbeil.hhc.client.controller.ItemEditorController.OnClickAddItemCallback;

public class ItemEditorWidget extends LayoutContainer {

	private static final ItemEditorMessages MESSAGES = GWT
			.create(ItemEditorMessages.class);
	private LayoutContainer rootContainer;
	private OnClickAddItemCallback onClickAddItem;
	private TextBox itemPrice;
	private TextBox itemDescription;

	public ItemEditorWidget() {
		createWidgets();
	}

	public Widget getWidget() {
		return this;
	}

	public void setOnClickAddItemCallback(OnClickAddItemCallback callback) {
		this.onClickAddItem = callback;
	}

	private void createWidgets() {
		addRootContainer();
		addHeader();
	}

	private void addRootContainer() {
		rootContainer = new LayoutContainer();
		rootContainer.addStyleName("hhc-item-edit-root-container");
		add(rootContainer);
	}

	private void addHeader() {
		LayoutContainer headerContainer = new LayoutContainer(
				new ColumnLayout());
		headerContainer.addStyleName("hhc-item-edit-root-container-header");
		addItemDescription(headerContainer);
		addItemPrice(headerContainer);
		addAddButtom(headerContainer);
		rootContainer.add(headerContainer);
	}

	private void addAddButtom(LayoutContainer headerContainer) {
		Button addButton = new Button(MESSAGES.add());
		headerContainer.add(addButton);
		addButton.addSelectionListener(new SelectionListener<ButtonEvent>() {
			@Override
			public void componentSelected(ButtonEvent ce) {
				onClickAddItem.addItem(itemDescription.getText(),
						itemPrice.getText());
			}
		});
	}

	private void addItemPrice(LayoutContainer headerContainer) {
		itemPrice = new TextBox();
		itemPrice.setText("50,00");
		headerContainer.add(itemPrice);
	}

	private void addItemDescription(LayoutContainer headerContainer) {
		itemDescription = new TextBox();
		itemDescription.setText(MESSAGES.description());
		itemDescription.setFocus(true);
		itemDescription.selectAll();
		headerContainer.add(itemDescription);
	}
}
